<template>
  <header class="header-container">
    <div class="header-left">
      <el-button 
        type="primary" 
        circle 
        @click="toggleSidebar" 
        size="mini"
      >
        <el-icon v-if="isCollapse">
          <Expand />
        </el-icon>
        <el-icon v-else>
          <Fold />
        </el-icon>
      </el-button>
    </div>
    <div class="header-center">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item 
          v-for="(crumb, index) in breadcrumbs" 
          :key="index"
          :to="crumb.path ? { path: crumb.path } : null">
          <el-icon v-if="index === 0" class="breadcrumb-icon"><HomeFilled /></el-icon>
          <span>{{ crumb.label }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="header-right">
      <div class="header-item">
        <el-badge :value="12" class="item">
          <Bell class="icon-small" />
        </el-badge>
      </div>
      <div class="header-item">
        <el-dropdown>
          <div class="user-info">
            <el-avatar icon="UserFilled" size="small"></el-avatar>
            <span class="user-name" v-if="!isCollapse">张老师</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <UserFilled />
                <span>个人信息</span>
              </el-dropdown-item>
              <el-dropdown-item>
                <Setting />
                <span>账户设置</span>
              </el-dropdown-item>
              <el-dropdown-item>
                <Clock />
                <span>登录记录</span>
              </el-dropdown-item>
              <el-dropdown-item divided>
                <SwitchButton />
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import {
  UserFilled,
  Setting,
  Clock,
  SwitchButton,
  Bell,
  Fold,
  Expand
} from '@element-plus/icons-vue'

// 接收父组件传递的属性
const props = defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})

// 定义事件用于通知父组件
const emit = defineEmits(['toggle-sidebar'])

// 面包屑数据
const route = useRoute()
const breadcrumbs = computed(() => {
  // 生成面包屑导航数据
  const matched = route.matched.filter(item => item.meta && item.meta.title)
  
  // 总是先添加仪表盘
  const result = [{
    path: '/dashboard',
    label: '仪表盘'
  }]
  
  // 如果当前页面不是仪表盘，则添加当前页面的分类和路径
  if (matched.length > 0 && matched[0].path !== '/dashboard') {
    // 添加分类
    const category = matched[0].meta.categoryLabel
    if (category) {
      result.push({
        path: null, // 分类没有具体路径
        label: category
      })
    }
    
    // 添加当前页面
    result.push({
      path: matched[0].path,
      label: matched[0].meta.title
    })
  }
  
  return result
})

// 切换侧边栏事件
const toggleSidebar = () => {
  emit('toggle-sidebar')
}
</script>

<style scoped>
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px; /* 减小导航栏高度 */
  background-color: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 21, 41, 0.08);
  padding: 0 15px; /* 减少内边距 */
  z-index: 1000;
  width: 100%;
  box-sizing: border-box;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-center {
  flex: 1;
  margin: 0 12px;
  font-size: 13px; /* 缩小面包屑字体 */
}

.header-right {
  display: flex;
  align-items: center;
}

.header-item {
  margin-left: 15px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-name {
  margin-left: 8px;
  font-size: 13px;
  color: #606266;
}

.icon-small {
  font-size: 14px;
  width: 14px;
  height: 14px;
}
</style>